package cn.org.book.shop.article.service.impl;

import cn.org.book.shop.article.domain.ApArticleConfig;
import cn.org.book.shop.article.domain.ApArticleDataConfiguration;
import cn.org.book.shop.article.mapper.ApArticleConfigMapper;
import cn.org.book.shop.article.mapper.ApArticleDataConfigurationMapper;
import cn.org.book.shop.article.mapper.ApArticleMapper;
import cn.org.book.shop.article.mapper.ApUserMapper;
import cn.org.book.shop.article.service.IApArticleDataConfigurationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 文章数据配置Service业务层处理
 *
 * @author wanghongjie
 * @date 2020-11-30
 */
@Service
public class ApArticleDataConfigurationServiceImpl implements IApArticleDataConfigurationService {
    @Resource
    private ApArticleDataConfigurationMapper apArticleDataConfigurationMapper;
    @Resource
    private ApArticleConfigMapper apArticleConfigMapper;
    @Resource
    private ApArticleMapper apArticleMapper;
    @Resource
    private ApUserMapper apUserMapper;

    /**
     * 查询文章数据配置列表
     *
     * @param apArticleDataConfiguration 文章数据配置
     * @return 文章数据配置
     */
    @Override
    public List<ApArticleDataConfiguration> selectApArticleDataConfigurationList(ApArticleDataConfiguration apArticleDataConfiguration) {
        List<ApArticleDataConfiguration> collect = apArticleDataConfigurationMapper.selectApArticleDataConfigurationList(apArticleDataConfiguration)
                .stream().filter(this::isDown).collect(Collectors.toList());
        collect.forEach(item -> {
            item.setApArticle(apArticleMapper.selectApArticleById(item.getArticleId()));
            ApArticleConfig apArticleConfig;
            item.setArticleConfig((apArticleConfig = apArticleConfigMapper.selectApArticleConfigByArticleId(item.getArticleId())) != null ? apArticleConfig : null);
        });
        return collect;
    }

    public List<ApArticleDataConfiguration> selectTotal(ApArticleDataConfiguration apArticleDataConfiguration) {
        return apArticleDataConfigurationMapper.selectApArticleDataConfigurationList(apArticleDataConfiguration);
    }

    /**
     * 判断文章是否下架
     *
     * @param dataConfiguration 是否下架判断
     * @return
     */
    private boolean isDown(ApArticleDataConfiguration dataConfiguration) {
        ApArticleConfig apArticleConfig;
        return (apArticleConfig = apArticleConfigMapper.selectApArticleConfigByArticleId(dataConfiguration.getArticleId())) != null && !apArticleConfig.getIsDown();
    }

    /**
     * 查询文章数据配置
     *
     * @param id 文章数据配置ID
     * @return 文章数据配置
     */
    @Override
    public ApArticleDataConfiguration selectApArticleDataConfigurationById(Long id) {
        return apArticleDataConfigurationMapper.selectApArticleDataConfigurationById(id);
    }


    /**
     * 新增文章数据配置
     *
     * @param apArticleDataConfiguration 文章数据配置
     * @return 结果
     */
    @Override
    public int insertApArticleDataConfiguration(ApArticleDataConfiguration apArticleDataConfiguration) {
        return apArticleDataConfigurationMapper.insertApArticleDataConfiguration(apArticleDataConfiguration);
    }

    /**
     * 修改文章数据配置
     *
     * @param apArticleDataConfiguration 文章数据配置
     * @return 结果
     */
    @Override
    public int updateApArticleDataConfiguration(ApArticleDataConfiguration apArticleDataConfiguration) {
        return apArticleDataConfigurationMapper.updateApArticleDataConfiguration(apArticleDataConfiguration);
    }

    /**
     * 批量删除文章数据配置
     *
     * @param ids 需要删除的文章数据配置ID
     * @return 结果
     */
    @Override
    public int deleteApArticleDataConfigurationByIds(Long[] ids) {
        return apArticleDataConfigurationMapper.deleteApArticleDataConfigurationByIds(ids);
    }

    /**
     * 删除文章数据配置信息
     *
     * @param id 文章数据配置ID
     * @return 结果
     */
    @Override
    public int deleteApArticleDataConfigurationById(Long id) {
        return apArticleDataConfigurationMapper.deleteApArticleDataConfigurationById(id);
    }
}
